package AEF::EntityGrabber;

use Modern::Perl;
use Moose;
use Log::Log4perl qw( :easy );

require AEF::AEFProcessor;
require AEF::Entity;

extends 'AEF::AEFProcessor';

has 'entities' => (
	is => 'rw', 
	isa => 'ArrayRef[AEF::Entity]', 
	default => sub { [] }
);
has 'first' => (
    is => 'rw',
    isa => 'Bool',
    default => 1,
);

has '+typeFormats' => (
	default => sub { 
		{
			0x40 => 'Z*',
		};
	}
);

around 'RequireDataForType' => sub
{
	my $orig = shift;
	my $self = shift;
	my $type = shift;

	my $logger = get_logger();
	my $require = $self->$orig($type);
	if($require)
	{
        if($type != 0x40)
        {
            $require = 0;
        }
		if($type == 0x20)
		{
            if(!$self->first)
            {
                $self->doneProcessing(1);
            }
            else
            {
                $self->first(0);
            }
		}
	}
	return $require;
};

override 'ProcessBlock' => sub 
{
	my $self = shift;
	my ($type, $data_chunk, $ptr) = @_;
	my $logger = get_logger();
	given($type)
	{
		when(0x40)
		{
			my ($entity_name) = $data_chunk;
			my $entity = AEF::Entity->new(
				name => $entity_name,
				ptr => $ptr,
			);
			$logger->debug("Found entity $entity_name");
			push @{$self->entities}, $entity;
		}
	}
};


1;

no Moose;

